/*
 * Copyright 2011 OpenWAF.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.openwaf.client.ui.widget;

import com.openwaf.client.core.Window;
import com.openwaf.client.dom.OListElement;
import com.openwaf.client.event.HandlerRegistration;
import com.openwaf.client.event.dom.ClickEvent;
import com.openwaf.client.event.dom.ClickHandler;
import com.openwaf.client.ui.UIControl;
import com.openwaf.client.ui.annotation.ViewMethod;
import com.openwaf.client.ui.annotation.ViewProperty;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public abstract class MenuBase extends UIControl implements Iterable<MenuItem>{

    
    public static String CSS_MENU_ITEM="waf-widget-menu-item;";
    public static String CSS_MENU_ITEM_HOVER="waf-widget-menu-item-selected;";
    
    private OListElement olChilds;
    private Orientation orientation;
    private ArrayList<MenuItem>  menusToHide;
    private ArrayList<MenuItem> childs;
    private boolean globalClickHandlerAdded;
    private ClickHandler globalClickHandler;
    HandlerRegistration globalClickReg;
    public MenuBase(){
        globalClickHandlerAdded=false;
        childs=new ArrayList<MenuItem>();
        menusToHide=new ArrayList<MenuItem>();
        orientation=Orientation.HORIZONTAL;
        globalClickHandler=new ClickHandler() {
            public void onClick(ClickEvent event) {
                hideAllMenus();
            }
        };
    }
    public enum  Orientation {
        HORIZONTAL,VERTICAL
    }

    @ViewProperty
    public void setOrientation(String value){
        if(value.equals("vertical")){
            setOrientation(Orientation.VERTICAL);
        }else{
            setOrientation(Orientation.HORIZONTAL);
        }
    }
    public void setOrientation(Orientation value){       
        orientation=value;
        /*
        if(orientation==Orientation.HORIZONTAL){
            olChilds.getStyle().setDisplay("inline");
        }else{
            olChilds.getStyle().setDisplay("");
        }*/
    }
    public Orientation getOrientation(){
        return orientation;
    }
    public boolean isHorizontal(){
        return orientation == Orientation.HORIZONTAL;
    }
    public boolean isVertical(){
        return orientation == Orientation.VERTICAL;
    }
    public void addMenuToHide(MenuItem item){
        int a=0;        
        ArrayList<MenuItem> toRemove=null;
        for(MenuItem i:menusToHide){
            if(i.getIndex()==item.getIndex()){
                toRemove=new ArrayList<MenuItem>();
                for(MenuItem j:menusToHide){
                    if(j.getIndex()>=i.getIndex()){
                        toRemove.add(j);
                    }
                }
                break;
            }
        }
        if (toRemove != null) {
            for (MenuItem j : toRemove) {
                j.hide();
                menusToHide.remove(j);
            }
        }
        menusToHide.add(item);
        if(!globalClickHandlerAdded){
             globalClickReg=Window.getDocument().addClickHandler(globalClickHandler);
             globalClickHandlerAdded=true;
        }        
    }
    protected void hideAllMenus(){
        for(MenuItem item:menusToHide){
            item.hide();
        }
        globalClickReg.removeHandler();
        globalClickHandlerAdded=false;
    }
    @ViewMethod
    public void addChild(MenuItem item){
        childs.add(item);
        item.setRoot(true,this);
        if(isHorizontal()){
            item.getElement().getStyle().setFloat("left");
        }else{
            item.getElement().getStyle().setFloat("");
        }
        olChilds.appendChild(item.getElement());

    }
    public void addChild(int index,MenuItem item){
        if(index<0 || index> childs.size()){
            throw new IndexOutOfBoundsException("Index out of range in MenuBar::addMenuItemAt");
        }
        MenuItem exitem=childs.get(index);
        childs.add(item);
        if(isHorizontal()){
            item.getElement().getStyle().setFloat("left");
        }else{
            item.getElement().getStyle().setFloat("");
        }
        item.setRoot(true,this);
        olChilds.insertBefore(item.getElement(),exitem.getElement());
    }
    public boolean removeChild(MenuItem item){
        if(!childs.contains(item)){
            return false;
        }
        childs.remove(item);
        return olChilds.removeChild(item.getElement())!=null;
    }
     public boolean removeChild(int index){
        if(index<0 || index> childs.size()){
            throw new IndexOutOfBoundsException("Index out of range in MenuBar::removeMenuItemAt");
        }
        MenuItem item=childs.get(index);
        childs.remove(item);
        return olChilds.removeChild(item.getElement())!=null;
    }
    public Iterator<MenuItem> iterator(){
        return childs.iterator();
    }
    

}
